package com.xm.danmaku.dto;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

@Data
public class DanmakuMessage {

    /**
     * 房间ID
     */
    private String roomId;

    /**
     * 弹幕内容
     */
    private String content;

    /**
     * 用户ID
     */
    private String userId;


    /**
     * 用户昵称
     */
    private String nickname;

    /**
     * 弹幕颜色
     */
    private String color;

    /**
     * 弹幕字体大小
     */
    private Integer size;

    /**
     * 时间戳
     */
    private Long timestamp;

    /**
     * 弹幕类型 0：用户发送 1：系统回复
     */
    private String type;

    public DanmakuMessage() {
    }

    // 简化构造方法
    public DanmakuMessage(String roomId, String content, String userId) {
        this.roomId = roomId;
        this.content = content;
        this.userId = userId;
        this.color = "#ffffff";
        this.size = 16;
        this.timestamp = System.currentTimeMillis();
    }

    public String toJson() {
        try {
            return new ObjectMapper().writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("序列化失败", e);
        }
    }

    public static DanmakuMessage fromJson(String json) {
        try {
            return new ObjectMapper().readValue(json, DanmakuMessage.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("反序列化失败", e);
        }
    }
}
